﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using hcom;
using System.Data;
using System.Text;

namespace hcms.Admin
{
	public partial class TemplateEdit : System.Web.UI.Page
	{
		public string submitstate;
		public string type;
		//public string addedit2;
		//private string id;
		private hString hstr = new hString();
		public Model model = Global.model;
		protected void Page_Load(object sender, EventArgs e)
		{
			if (Request["id"] != "0")			// 0 表示新建
	htmaddedit.InnerText = Request["id"];

			type = Request["type"];	 //?? 开发机上的IE8地址栏的编码是GB2312，汉字参数取出是乱码，只好全部用英文。
			if (Page.IsPostBack)
			{
	switch (cmd.Value)
	{
		case "save":
			save();
			updateParts();	// 如果当前是板块，更新板块
			submitstate = "保存完毕";
			return;
			//break;
		default:
			throw (new Exception("不可识别的命令: \"" + cmd.Value + "\""));
	}
			}

			code.Value = File.ReadAllText(getFolder());

			if ( htmaddedit.InnerText != "0" )
			{
				DataRow row = model.getTemplateByID(htmaddedit.InnerText)[0];
	idname.Value = ((string)row["temName"]).Trim();
	description.Value = (string)row["temDescription"];
			}
		}

		private void save()
		{
			if (htmaddedit.InnerText == "0")	// 创建新模板
			{
	string strsql = string.Format("insert into hcms_Templates(temName, temDescription, temType) values('{0}', '{1}', '{2}')",
		hstr.ToSQL(idname.Value),
		hstr.ToSQL(description.Value),
		type
		);
	htmaddedit.InnerText = model.da.ExecuteReturnID(strsql).ToString();
	//path = Request.PhysicalApplicationPath + "Templates/m" + id + ".Template";		//取网站虚拟目录的实际文件夹
	File.WriteAllText(
		getFolder(),
		code.Value,	//.Replace("&1l1t1;", "<"),	// 前台用JS把"<"换成了"&1l1t1;"，避开安全验证，取消安全安全验证后出其它的异常。换成“&lt;”后会被自动换回来。
		System.Text.Encoding.GetEncoding("utf-8"));
	// 重定向到列表页
	//string str = string.Format("<script>window.location='TemplateList.aspx?type={0}';</script>",
	//	type
	//	);
	//Response.Write(str);
			}
			else
			{
	string strsql = string.Format("UPDATE hcms_Templates SET temName = '{0}', temDescription = '{1}' WHERE temID = '{2}'",
		hstr.ToSQL(idname.Value),
		hstr.ToSQL(description.Value),
		htmaddedit.InnerText
		);
	model.da.Execute(strsql);
	File.WriteAllText(
		getFolder(),
		code.Value, //.Replace("&1l1t1;", "<"),
		System.Text.Encoding.GetEncoding("utf-8"));

	// 如果是文档模板，修改模板后把对应的文档ID置成0，下次打开文档时重新生成。
	if (type == "a")
	{
		model.delFilesTid(htmaddedit.InnerText);
	}
			}

		}

		private string getFolder()
		{
			switch (type)
			{
	case "t":
		return Request.PhysicalApplicationPath + "Templates/t" + htmaddedit.InnerText + ".aspx";		//取网站虚拟目录的实际文件夹
	case "a":
		return Request.PhysicalApplicationPath + "Templates/a" + htmaddedit.InnerText + ".aspx";		//取网站虚拟目录的实际文件夹
	case "d":
		return Request.PhysicalApplicationPath + "Templates/d" + htmaddedit.InnerText + ".ascx";		//取网站虚拟目录的实际文件夹
	default:
		throw (new Exception("不可识别的模板类型: " + type));
			}

		}

		// 更新板块
		private void updateParts()
		{
			if (type != "d")	// 不是板块类型时不更新板块
				return;

			string str = string.Format("UpdateParts.aspx?id={0}", htmaddedit.InnerText);	// 更新板块
			StringBuilder buffer = new StringBuilder(10000);	// 建立临时缓冲
			using (StringWriter stringWriter = new StringWriter(buffer, System.Globalization.CultureInfo.InvariantCulture))
			{
				Server.Execute(str, stringWriter);	// 运行板块控件，输出到临时缓冲中，不输出到当前页面。
			}

		}

	}
}